package com.hyts.rpclet.util;

import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationObjectSupport;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.rpclet.util
 * author:Libo/Alex
 * create-date:2019-07-09 21:55
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    @Getter
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    /**
     * 查询getBean对象
     * @param idOrName
     * @return
     */
    public static Object getBean(String idOrName){
        return applicationContext.getBean(idOrName);
    }

    /**
     * 查询getBean对象
     * @param idOrName
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String idOrName,Class<T> clazz){
        return applicationContext.getBean(idOrName,clazz);
    }

    /**
     * 查询GetBean对象
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }
}
